var t = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
		iconsUrl:t.globalData.iconsUrl,
		 // 个人信息修改弹窗
		 showPopup:false,
		 birthday:'',
		 avatarUrl:'',
		 avatarbase64:'',
		 info:{},
		 sexList:[{id:1,name:'男'},{id:2,name:'女'}],
		 sex_index:0,
		 phone:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
	},
	 // 选择头像
	 onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    console.log(e.detail.avatarUrl);
    let av=e.detail.avatarUrl;
    let av1=wx.getFileSystemManager().readFileSync(av,'base64');
    let avatarUrl1 = "data:image/jpeg;base64,"+av1;
    console.log(avatarUrl1)
    this.setData({
      avatarUrl,
      avatarbase64:avatarUrl1
    })
  },
  //显示user编辑弹窗
  onShowPopup(){
    let that = this;
    that.setData({
      showPopup: !that.data.showPopup
    })
  },
  //提交保存信息（编辑user）
  formSubmit(e) {
    let that = this
		let formD = e.detail.value;//获取表单数据
		let obj = {
			avatar:that.data.avatarUrl?that.data.avatarbase64:'',
			birthday:that.data.birthday,
			nick_name:formD.nick_name,
			sex:that.data.sexList[that.data.sex_index].id,
			phone:''
		}
    t.request('/Users/upUserInfo',obj,function(e){
			if(e.code!=200) return 
			wx.showToast({ title:e.msg,icon:"none" })
			that.getData()
      that.onShowPopup()
    })
	},
	// 提交保存电话
	submitPhone(phone){
		let that = this
		let obj = {phone}
    t.request('/Users/upUserInfo',obj,function(e){
			if(e.code!=200) return 
			wx.showToast({ title:e.msg,icon:"none" })
			that.getData()
    })
	},
	// 性别
	bindSexChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			sex_index: e.detail.value
		})
	},
  // 生日
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      birthday: e.detail.value
    })
  },
  //获取数据列表
  getData(){
		let that = this
    t.request('/users/info',{},function(e){
      if (e.code==200) {
        that.setData({
					info:e.result,
					sex_index:e.result.sex!=''?e.result.sex-1:0,
					birthday:e.result.birthday,
					phone:''
        })
      }
    })
  },
  //电话
  getPhoneNumber(e){
    const that = this,
		iv = e.detail.iv,
		da = e.detail.encryptedData;
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    // 请求接口
    wx.login({
      success(e){
        wx.showLoading({title: '请稍后'})
        t.request('/login/getPhoneNumber',{code: e.code,iv:iv,encryptedData:da},function(event){
          wx.hideLoading({})
          if (event.code!=200) return wx.showToast({ title: event.msg,icon:'none' })
          if (event.code==200) {
						that.setData({phone:event.result});
						that.submitPhone(event.result)
            wx.setStorageSync('tel', event.result)
            t.globalData.show_tel_login =false
          }
        })
      }
    })
	},
	notPhoneNumber(){
		wx.showModal({
			title: '友情提示',
			content: '手机号每6个月可修改一次',
			showCancel:false
		})
	},
  // 自定义下拉刷新
  upRefresh(){
		this.getData()
		wx.stopPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
   //调用刷新时将执行的方法
   this.upRefresh();
   setTimeout(function () {
    // 不加这个方法真机下拉会一直处于刷新状态，无法复位
    wx.stopPullDownRefresh()
   }, 1000)
  }, 

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})